const { salesModel } = require("../models/salesModels");


// 查询该销售员账号是否已被注册
module.exports.searchSales = async function (salesUsername) {
    return await salesModel.find(salesUsername);
}

// 新增销售员
module.exports.addSales = async function (sales) {
    return await salesModel.create(sales);
}

// 获取所有销售员数据
module.exports.getSales = async function ({ pageSize, currentPage }) {
    // 获取销售员数据
    const total = await salesModel.countDocuments({ delete: false });
    // 计算总页数
    const pages = Math.ceil(total / pageSize);
    const sales = await salesModel
        .find({ delete: false })
        .limit(pageSize - 0)//设置请求数据的条数
        .skip((currentPage - 1) * pageSize);//设置跳过数据的条数
    return {
        total, pages, sales
    }
}


// 通过id获取该销售员数据
module.exports.getSalesById = async function (params) {
    return await salesModel.find(params);
}



// 软删除销售员
module.exports.deleteSales = async function (_id) {
    return await salesModel.updateOne(_id, { delete: true });
}


// 修改销售员数据
module.exports.modifySales = async function (sales) {
    const data = await salesModel.updateOne({ _id: sales._id }, sales);
    return data;
}

// 查看该销售员负责的客户列表
module.exports.getAllCustomersBelongToTheSales = async function ({ customersPageSize, customersCurrentPage, _id }) {
}
//获取一个销售员
module.exports.getSaleOne = async (val)=>{
    const data = await salesModel.find(val)
    return data
}
//修改一个销售员
module.exports.upDataOne =async (val) =>{
    const data = await salesModel.updateOne({_id:val._id},{username:val.username,password:val.password})
    return data
}

